;
(function (window, angular) {
	'use strict';
	angular.module("Controller.Stock.StorageFlow", [])
		.controller("StorageFlowCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "UserContextService", "Upload", "$timeout", "EventBus", "$filter", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, UserContextService, Upload, $timeout, EventBus, $filter) {
			$scope.markInfo = {
				allChecked: false,
				curRow: 0
			};
			$scope.pageInfo = {
				totalPages: 1,
				totalRows: 0,
				pageSize: 10,
				curPage: 1
			};
			$scope.dataList = [];
			$scope.condition = {};
			$scope.toolbarConfig = {
				showAdvanceSearch: false,
				showColConfig: false
			};

			$scope.exportParam = {
				sectionParam: {},
				allUrl: ybHttp.RequestUrl("device", "/warehouseInventory/storageFlowExport")
			};

			$scope.tableConfig = {
				fixedWidth: 40,
				scrollWidth: 700,
				tableCol: [
					{
						name: "零件编号",
						dataField: "materialsNo",
						width: 150,
						isShow: true,
						isFixed: false,
					},
					{
						name: "零件中文名",
						dataField: "materialName",
						width: 300,
						isShow: true,
						isFixed: false,
					},
					{
						name: "操作数量汇总",
						dataField: "amount",
						width: 150,
						isShow: true,
						isFixed: false,
					},
					{
						name: "计量单位",
						dataField: "unit",
						width: 100,
						isShow: true,
						isFixed: false,
					},
				]
			};

			$scope.setFixedWidth = function () {
				$scope.tbStyle = {
					billFixedWidth: {left: $scope.tableConfig.fixedWidth + "px"}
				};
			};

			function getDataList(curPage) {
				var service = "device";
				var reqUrl = "/warehouseInventory/storageFlow";
				var param = {};
				param.pageSize = $scope.pageInfo.pageSize;
				if (!!curPage) {
					$scope.pageInfo.curPage = curPage;
				}
				param.pageNum = $scope.pageInfo.curPage;
				param.filters = $scope.condition;
				ybHttp.Post(service, reqUrl, param).then(function (resp) {
					if (resp.code !== 0) {
						ybWidget.ShowMessage(resp, MESG_TYPE.Error);
						return;
					}
					angular.forEach(resp.value.list, function (item) {
						item.isChecked = false;
						item.createDateShow = item.createDateStr;
						item.businessSingleDes=$filter('orderType')(item.businessSingle);
                        item.frozernStatusShow = item.frozernStatus===1?"是":"否";
					});
					$scope.pageInfo.totalRows = resp.value.total;
					$scope.pageInfo.totalPages = resp.value.pages;
					$scope.dataList = resp.value.list;
				});
			}

			$scope.datePickerFlag = [false, false];
			$scope.switchDataPicker = function (index) {
				$scope.datePickerFlag = [false, false];
				if (angular.isDefined(index)) {
					$scope.datePickerFlag[index] = true;
				}
			};
			$scope.checkDate = function (obj, startFiled, endFiled) {
					var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
					if (!!timeFormatObj.tStart) {
							obj[startFiled] = timeFormatObj.tStart;
					}
					if (!!timeFormatObj.tEnd) {
							obj[endFiled] = timeFormatObj.tEnd;
					}
			};

			$scope.switchToolbar = function (toolbar) {
				if (!!$scope.toolbarConfig["show" + toolbar]) {
					$scope.toolbarConfig["show" + toolbar] = false;
				} else {
					$timeout(function () {
						$scope.toolbarConfig["show" + toolbar] = true;
					});
				}
			};

			$scope.selectRow = function (index) {
				$scope.markInfo.cueRow = index
			};

			$scope.search = function (curpage) {
				getDataList(curpage);
			};

			$scope.reset = function () {
				$scope.condition = {};
			};

			function geBillIds() {
				$scope.exportParam.sectionParam.ids = [];
				angular.forEach($scope.dataList, function (item) {
					if (!!item.isChecked) {
						$scope.exportParam.sectionParam.ids.push(item.id);
					}
				});
			}
			function checkAll() {
				var selectAll = false;
				for (var i = 0, len = $scope.dataList.length; i < len; i++) {
					if (!$scope.dataList[i].isChecked) {
						selectAll = false;
						break;
					}
					selectAll = true;
				}
				return selectAll;
			}

			$scope.selectSingle = function (item) {
				if (!item.isChecked) {
					$scope.markInfo.allChecked = false;
				} else {
					if (!checkAll()) {
						$scope.markInfo.allChecked = false;
					} else {
						$scope.markInfo.allChecked = true;
					}
				}
				geBillIds();
			};

			$scope.selectAll = function () {
				for (var i = 0, len = $scope.dataList.length; i < len; i++) {
					$scope.dataList[i].isChecked = $scope.markInfo.allChecked;
				}
				geBillIds();
			};

			(function () {
				$scope.setFixedWidth();
			})();
		}])
	;
})(window, window.angular);